const {request} = require("../utils/request")
const UserBo = require('./user-bo')
module.exports = class FinanceBo extends UserBo {
    async getBalance() {
        let params = this.getCommonParams()
        let ret = await request({
            url: '/api/api.php/protocol/101019',
            method: 'post',
            responseType: 'json',
            formData: params,
            headers: this.headers
        })
        if (ret.status == 200) {
            return ret.data.balance
        }
        return 0
    }

    async getRecords() {
        let params = this.getCommonParams()
        let ret = await request({
            url: '/api/api.php/protocol/101021',
            method: 'post',
            responseType: 'json',
            formData: params,
            headers: this.headers
        })
        if (ret.status == 200) {
            return ret.data.list
        }
        return []
    }

    async withdraw(money) {
        let params = this.getCommonParams()
        params['money'] = money
        let ret = await request({
            url: '/api/api.php/protocol/101020',
            method: 'post',
            responseType: 'json',
            formData: params,
            headers: this.headers
        })
        return ret.status == 200;


    }


    async calculateUserMoney(){
        let records = await this.getRecords();
        let total_money = 0;
        let balance = 0;
        let withdraw = 0;
        if (records.length > 0) {
            records.forEach((item, index) => {
                if (item['money_pm_sign'] === "+") {
                    total_money += Number(item['money'])
                } else if (item['money_pm_sign'] === "-") {
                    withdraw += Number(item['money'])
                }
            })
        }
        total_money = parseFloat(total_money.toFixed(2))
        withdraw = parseFloat(withdraw.toFixed(2))
        balance = parseFloat((total_money - withdraw).toFixed(2));

        return {
            'total_money': total_money,
            'withdraw': withdraw,
            'balance': balance,
            'records':records
        }
    }
}